package com.campus.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.campus.entity.Article;
import com.campus.entity.Discuss;
import com.campus.entity.MyFile;
import com.campus.entity.User;
import com.campus.service.ArticleService;
import com.campus.service.MyFileService;
import com.campus.service.UserSercvice;
import com.campus.util.DateUtil;
import com.campus.util.FileUtil;
import com.campus.util.SessionUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName ActionSupport
 * @Description �ļ���Ϣ����
 * @author guodayang
 * @date 2018��3��13�� 10:43:23
 */
public class ArticleAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	protected final String JUMP = "jump";// ��ת
	private String resultJson;// ������� 1��Ч/-1ʧ��/2��ǰҳ��

	@Resource
	private ArticleService articleService;
	@Resource
	private UserSercvice userSercvice;
	@Resource
	private MyFileService myFileService;

	private com.campus.vo.Article articleVo;
	private List<com.campus.vo.Article> listVo;
	// private com.campus.vo.MyFile myfileVo;

	private Article article;
	private int id;// id
	private int userid = SessionUtil.getMyUserSession().getId();
	private List<Article> list;

	private File file;
	private String fileContentType;
	private String fileFileName;
	private String destPath;
	private String newdate;

	public String articleAdd() {
		// �ж�����ʽ
		if ("post".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())) {

			article.setUser(userSercvice.getById(SessionUtil.getMyUserSession().getId()));// ������
			article.setDate(new java.util.GregorianCalendar().getTime());// ����ʱ��
			if (file != null) {
				File file1 = myFileService.addArticleFile(file);
				MyFile myfile = new MyFile();
				myfile.setName(file1.getName());
				myfile.setSize(FileUtil.GetFileSize(file1));
				myfile.setType("action-img");
				myFileService.add(myfile);
				Set<MyFile> myFiles = new HashSet<MyFile>();
				myFiles.add(myfile);
				article.setMyFiles(myFiles);
			}
			articleService.add(article);
			id = article.getId();// ��ֵid ����ҳ��
			resultJson = "1";
			return SUCCESS;
		} else {
			return JUMP;
		}

	}


	public String articleOne() {
		if ("post".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())) {
			Article articleNew = articleService.getById(id);

			// ����fileVo
			Set<com.campus.vo.MyFile> myFilesVo = new HashSet<com.campus.vo.MyFile>();
			for (MyFile myfile : articleNew.getMyFiles()) {
				com.campus.vo.MyFile myfilevo = new com.campus.vo.MyFile(null, null, myfile.getName(), null, null);
				myFilesVo.add(myfilevo);
			}

			// ����joinUserVo
			Set<com.campus.vo.User> joinUserVo = new HashSet<com.campus.vo.User>();
			for (User user : articleNew.getJoinUser()) {
				com.campus.vo.User user2 = new com.campus.vo.User(user.getId(), null, null, null, null, null,
						new com.campus.vo.MyFile(null, null, user.getMyFile().getName(),null,null),
						null, null, null, null, null);
				joinUserVo.add(user2);
			}
		
			// ����joinUserVo
			Set<com.campus.vo.Discuss> discussesVo = new HashSet<com.campus.vo.Discuss>();
			for (Discuss d : articleNew.getDiscusses()) {
				Set<com.campus.vo.Discuss> GoalDiscussHashSet = new HashSet<com.campus.vo.Discuss>();
				for(Discuss d1 :d.getGoalDiscussHashSet()) {
					GoalDiscussHashSet.add(new com.campus.vo.Discuss(d1.getId()));
				}
				Set<com.campus.vo.User> goodUser = new HashSet<com.campus.vo.User>();
				for(User u :d.getGoodDiscuss()) {
					goodUser.add(new com.campus.vo.User(u.getId()));
				}
				
				com.campus.vo.Discuss d2 = new com.campus.vo.Discuss(d.getId(),
						new com.campus.vo.User(d.getThisUser().getId(), d.getThisUser().getName(), null, null, null,
								null,
								new com.campus.vo.MyFile(null, null, d.getThisUser().getMyFile().getName(), null, null),
								null, null, null, null, null),
						d.getDate(), d.getContent(),null, null, null,GoalDiscussHashSet);
				discussesVo.add(d2);
			}

			articleVo = new com.campus.vo.Article(articleNew.getId(), articleNew.getType(), articleNew.getTitle(),
					articleNew.getContent(), articleNew.getDate(), articleNew.getStartDate(), articleNew.getEndDate(),
					articleNew.getAddress(), articleNew.getPrice(),
					new com.campus.vo.User(articleNew.getUser().getId(), articleNew.getUser().getName(), null, null,
							null, null,
							new com.campus.vo.MyFile(null, null, articleNew.getUser().getMyFile().getName(), null,
									null),
							null, null, null, null, null),
					articleNew.getJoinUserNum(), myFilesVo, null, joinUserVo, discussesVo);
			return SUCCESS;
		}
		return JUMP;
	}

	public String articleList() {
		if ("post".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())) {
			newdate = DateUtil.getNowDate();
			List<com.campus.vo.Article> listVo2= new ArrayList<com.campus.vo.Article>();
			for(Article a :articleService.listActivitySort()) {
				Set<com.campus.vo.User> joinUserVo = new HashSet<com.campus.vo.User>();
				for(User u1 :a.getJoinUser()) {
					joinUserVo.add(new com.campus.vo.User(u1.getId()));
				}
				listVo2.add(new com.campus.vo.Article(a.getId(),a.getType(),a.getTitle(),null,a.getDate(),a.getStartDate(),a.getEndDate(),a.getAddress(),null,null,null,null,null,joinUserVo,null));
			}
			listVo=listVo2;
			return SUCCESS;
		}
		return JUMP;
	}

	public String articleJoinAdd() {
		User user = userSercvice.getById(userid);
		if (user == null) {
			resultJson = "-1";
		} else {
			Article article = articleService.getById(id);
			article.getJoinUser().add(user);
			articleService.update(article);
			resultJson = "1";
		}
		return SUCCESS;

	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public String getDestPath() {
		return destPath;
	}

	public void setDestPath(String destPath) {
		this.destPath = destPath;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getResultJson() {
		return resultJson;
	}

	public void setResultJson(String resultJson) {
		this.resultJson = resultJson;
	}

	public String getJUMP() {
		return JUMP;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public List<Article> getList() {
		return list;
	}

	public void setList(List<Article> list) {
		this.list = list;
	}

	public String getNewdate() {
		return newdate;
	}

	public void setNewdate(String newdate) {
		this.newdate = newdate;
	}

	public com.campus.vo.Article getArticleVo() {
		return articleVo;
	}

	public void setArticleVo(com.campus.vo.Article articleVo) {
		this.articleVo = articleVo;
	}


	public List<com.campus.vo.Article> getListVo() {
		return listVo;
	}


	public void setListVo(List<com.campus.vo.Article> listVo) {
		this.listVo = listVo;
	}

}
